package com.gsafety.grpc;

import com.gsafety.proto.*;
import io.grpc.Grpc;
import io.grpc.InsecureServerCredentials;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;

import java.io.IOException;
import java.sql.SQLOutput;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class GrpcServer {
    private Server server;

    private void start() throws IOException {
        this.server = Grpc.newServerBuilderForPort(8899, InsecureServerCredentials.create())
                .addService(new StudentServiceImpl())
                .build()
                .start();

        System.out.println("server started!");

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("close jvm");
            GrpcServer.this.stop();
        }));
    }

    private void stop() {
        if (null != this.server) {
            this.server.shutdown();
        }
    }

    private void awaitTermination() throws InterruptedException {
        if (null != this.server) {
           // this.server.awaitTermination(3000, TimeUnit.MILLISECONDS);
            this.server.awaitTermination();
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        final GrpcServer server = new GrpcServer();

        server.start();
        server.awaitTermination();
    }

    static class StudentServiceImpl extends StudentServiceGrpc.StudentServiceImplBase {

        @Override
        public void getRealNameByUsername(MyRequest request, StreamObserver<MyResponse> responseObserver) {

            MyResponse reply = MyResponse.newBuilder().setRealname("zhangsan").build();


            System.out.println("Received client information: " + request.getUsername());
            responseObserver.onNext(reply);
            responseObserver.onCompleted();
        }

        @Override
        public void getStudentsByAge(StudentRequest request, StreamObserver<StudentResponse> responseObserver) {
            System.out.println("Received client information: " + request.getAge());

            responseObserver.onNext(StudentResponse.newBuilder().setName("zhangdan").setAge(20).setCity("hefei").build());
            responseObserver.onNext(StudentResponse.newBuilder().setName("lisi").setAge(30).setCity("beijing").build());
            responseObserver.onNext(StudentResponse.newBuilder().setName("wanwu").setAge(40).setCity("shanghai").build());
            responseObserver.onNext(StudentResponse.newBuilder().setName("zhaoliu").setAge(70).setCity("guangdong").build());

            responseObserver.onCompleted();

        }


        @Override
        public StreamObserver<StudentRequest> getStudentsWrapperByAges(StreamObserver<StudentResponseList> responseObserver) {
            return new StreamObserver<StudentRequest>() {
                @Override
                public void onNext(StudentRequest studentRequest) {
                    System.out.println("onNext: " + studentRequest.getAge());
                }

                @Override
                public void onError(Throwable throwable) {
                    System.out.println(throwable.getMessage());
                }

                @Override
                public void onCompleted() {
                    StudentResponse studentResponse = StudentResponse.newBuilder().setName("zhangsan").setAge(20).setCity("beijing").build();
                    StudentResponse studentResponse2 = StudentResponse.newBuilder().setName("lisi").setAge(30).setCity("hefei").build();

                    StudentResponseList studentResponseList =  StudentResponseList.newBuilder().
                            addStudentResponse(studentResponse).addStudentResponse(studentResponse2).build();
                    responseObserver.onNext(studentResponseList);
                    responseObserver.onCompleted();

                }
            };
        }

        @Override
        public StreamObserver<StreamRequest> biTalk(StreamObserver<StreamResponse> responseObserver) {
            return new StreamObserver<StreamRequest>() {
                @Override
                public void onNext(StreamRequest streamRequest) {
                    System.out.println(streamRequest.getRequestInfo());

                    responseObserver.onNext(StreamResponse.newBuilder().setResponseInfo(UUID.randomUUID().toString()).build());
                }

                @Override
                public void onError(Throwable throwable) {
                    System.out.println(throwable.getMessage());
                }

                @Override
                public void onCompleted() {
                    responseObserver.onCompleted();
                }
            };
        }
    }
}


